﻿function CaseCentre() {
    var cc = this;
    cc.GetCurrentUser = function (currentUserUrl, callback) {
        $.ajax({
            url: currentUserUrl,
            dataType: "json",
            type: 'GET',
            success: function (result) {
                callback(result);
            }
        });
    };

    cc.printObject = function (obj) {
        var out = '';
        for (var p in obj) {
            out += p + ': ' + obj[p] + '\n';
        }
        return out;
    };

    cc.OnCommand = function (commandUrl, data, datatype, method, onSuccess, onError) {
        $.ajax({
            url: commandUrl,
            data: data,
            dataType: datatype,
            type: method,
            success: onSuccess,
            error: onError
        });
    };

    cc.DialogConfirm = function (message, options) {
        $('<div id="dialog-confirm" title="Case Centre"><p>' + message + '</p></div>').dialog(options);
    };

    cc.Accordion = function (ctrlId) {
        var icons = {
            header: "ui-icon-circle-arrow-e",
            activeHeader: "ui-icon-arrow-4-diag"
        };
        $(ctrlId).accordion({
            icons: icons,
            heightStyle: "content"
        });
    };

    cc.Datepicker = function (ctrlId) {
        $(ctrlId).datepicker({
            dateFormat: 'dd/mm/yy',
            showStatus: true,
            showWeeks: true,
            highlightWeek: true,
            numberOfMonths: 1
        });
    };

    cc.DatepickerWithYearMonth = function (ctrlId) {
        $(ctrlId).datepicker({
            dateFormat: 'dd/mm/yy',
            showStatus: true,
            showWeeks: true,
            highlightWeek: true,
            numberOfMonths: 1,
            changeMonth: true,
            changeYear: true
        });
    };

    // Buttons Binding
    cc.BindEdit = function (btnId, hdnId, editCommandUrl) {
        $(btnId).click(function () {
            var url = editCommandUrl + $(hdnId).val();
            document.location.href = url;
        });
    };

    cc.BindCancel = function (btnId, homeCommandUrl) {
        $(btnId).click(function () {
            document.location.href = homeCommandUrl;
        });
    };
    
    cc.BindButton = function (btnId, homeCommandUrl) {
        $(btnId).click(function () {
            document.location.href = homeCommandUrl;
        });
    };

    // Search
    cc.BindSearch = function (searchBtnId, searchText, searchType, searchUrl, onResult, onError) {

        $(searchBtnId).click(function() {
            var sText = $(searchText).val();
            var sType = $(searchType).val();

            cc.OnCommand(searchUrl,
                { SearchText: sText, SearchType: sType },
                'json',
                'POST',
                onResult,
                onError);
        });
    };

};